//依赖
import { ref, reactive , watch} from 'vue'
//路由依赖
import { useRouter } from 'vue-router'
//cnode请求参数变化
import { upDataParam, Pagination, updataPagin } from '@/store/tabParams'
import bus from '@/bus'
//定义tab栏目
const tabTitle = reactive({
  value: [
    { tab: '全部', path: 'all' },
    { tab: '招聘', path: 'job' },
    { tab: '精华', path: 'good' },
    { tab: '问答', path: 'ask' },
    { tab: '分享', path: 'share' },
    { tab: '客户端测试', path: 'dev' },
    { tab: '操作', path: 'newinsert' },
  ],
})
//控制tab样式
let index = sessionStorage.getItem('index') || 0
let currentIndex = ref(Number(index))
//控制页码show
if (currentIndex.value === tabTitle.value.length - 1) {
  updataPagin({
    value: false
  })
} else {
  updataPagin({
    data: true
  })
}
//bus的妙用
bus.on("detailsToEdit",(val) => {
  currentIndex.value  = val
})

//tab栏目操作功能
function handleTab(){
  //切换tab 更新数据
  const router = useRouter() //获取路由
  function switchTab(value, index) {
    currentIndex.value = index
    sessionStorage.setItem('index', index)
    sessionStorage.setItem('tab', value)
    upDataParam({
      page: 1,
      limit: 10,
      tab: value,
    })
    //tab切换/跳转
    if (index !== tabTitle.value.length - 1) {
        //页码控制show
        updataPagin({
          value: true
        })
        router.push({
          name: 'TabList',
        })
    } else {
        updataPagin({
          value: false
        })
        router.push({
          name: 'NewInsert',
        })
    }
  }
  return {
    tabTitle,
    currentIndex,
    Pagination,
    switchTab,
  }
}
export {
  handleTab
}